<template>
	<div class="homelist">
		<header>
			<h2>{{proptitle}}</h2>
			<router-link to="/">更多></router-link>
		</header>
		<ul>
			<li v-for="(item,index) in FilterList" :key="item.id">
				<router-link :to="`/mobile/bookdetail/${item.id}`"
					v-if="(item.hot != 0 && index < 3) || (item.price !=0 && index < 6)">
					<img :src="`http://127.0.0.1/api/public/showimg/${item.picture}`" />
					<h3>{{item.name}}</h3>
					<span v-show="item.price !=0">￥{{item.price.toFixed(2)}}</span>
					<span v-show="item.hot != 0">热度:{{item.hot}}</span>
				</router-link>
			</li>
		</ul>
	</div>
</template>

<script>
	export default {
		name: 'MobileHomeList',
		props: ['proplist', 'proptitle'],
		data() {
			return {
				list: []
			}
		},
		watch: {
			//如果数据是请求获取，基本上都需要监听props的数据，确保获取到数据后在进行循环等操作
			propslist() {
				this.list = this.proplist;
			}
		},
		// 做计算属性
		computed: {
			FilterList() {
				//现在是假数据，不存在时差，直接赋值【后期发起请求后必须删掉】
				this.list = this.proplist;
				console.log(this.proplist);
				let filters = this.list.map((item) => {
					return {
						id: item.id,
						name: item.memo ? item.memo : item.name,
						picture: item.picture ? item.picture : item.pic,
						price: !item.price ? 0 : item.price,
						hot: item.hot ? item.hot : 0
					}
				});
				filters.sort((a, b) => {
					return this.proptitle == '热门推荐' ? b.hot - a.hot : this.proptitle == '新书上市' ? b.id - a.id : a
						.id - b.id;
				})
				return filters;
			}
		}
	}
</script>

<style scoped>
	.homelist {
		width: 100%;
	}

	.homelist header {
		line-height: 2rem;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.homelist header h2 {
		color: white;
		font-size: 1rem;
		margin-left: 0.5rem;
	}

	.homelist header a {
		display: inline-block;
		width: 3rem;
		font-size: 1rem;
		color: white;
	}

	.homelist ul {
		width: 96%;
		margin: 0.5rem auto 1.5rem;
		display: flex;
		align-items: center;
		justify-content: space-between;
		flex-wrap: wrap;
	}

	.homelist ul li {
		width: 42%;
		overflow: hidden;
		margin-bottom: 0.5rem;
		margin: 0.2rem 0.55rem;
		position: relative;
		text-align: center;
		border-radius: 0.3rem;
		background-color: #393939;
	}

	.homelist ul li img {
		width: 100%;
		border-radius: 0.2rem;
		height: 11rem;
		/* margin-top: 1rem; */
	}

	.homelist ul li h3 {
		width: 100%;
		height: 2.5rem;
		line-height: 2.5rem;
		font-size: 0.9rem;
		/* background-color: rgba(245, 213, 160, 0.5); */
		/* color: #a68b5b; */
		color: white;
		text-align: center;
		/* 去掉加粗 */
		font-weight: normal;
		/* 强制不换行 */
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;

		/* 		支持webkit内核的浏览器才会生效
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2; */
	}

	.homelist ul li span {
		font-size: 0.8rem;
		background-color: rgba(0, 0, 0, 0.2);
		position: absolute;
		top: 0;
		right: 0;
		color: red;
		padding: 0.3rem 0.3rem;
		/* 单独给某个角设置圆角 */
		border-radius: 0 0 0 0.6rem;
	}
</style>
